package hlhj.fhp.supreme.customview

import android.content.Context
import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import hlhj.fhp.supreme.R
import kotlinx.android.synthetic.main.appoint_pop.view.*
import java.util.ArrayList

/**
 * Created by Administrator on 2018\4\8 0008.
 */
class AppointPop(private val context: Context,private val listener:OnPopClic,private val datas:ArrayList<String>) :PopupWindow() {
    private lateinit var adp:BaseQuickAdapter<String,BaseViewHolder>
    private var selectNum=-1;
    private val selectHash=HashMap<Int,Boolean>()
    init {
        this.width= LinearLayout.LayoutParams.WRAP_CONTENT
        this.height=LinearLayout.LayoutParams.WRAP_CONTENT
//        this.height=context.resources.displayMetrics.heightPixels/2
        setPop()
    }

    fun setPop(){
        animationStyle= R.style.class_pop_style
        contentView=LayoutInflater.from(context).inflate(R.layout.appoint_pop,null)
        for (i in 0..datas.size-1){
            selectHash.put(i,false)
        }
        adp=object :BaseQuickAdapter<String,BaseViewHolder>(R.layout.appointment_item,datas){
            override fun convert(helper: BaseViewHolder?, item: String?) {
                helper?.setText(R.id.itemTittle,item)
                val view = helper?.getView<TextView>(R.id.itemTittle)
                if (selectHash[helper?.adapterPosition]!!){
                    view?.setBackgroundColor(context.resources.getColor(R.color.them_yellow))
                }else{
                    view?.setBackgroundColor(context.resources.getColor(R.color.them_unsele))
                }
            }
        }
        contentView.listView.adapter=adp
        contentView.listView.layoutManager=LinearLayoutManager(context,LinearLayout.VERTICAL,false)

        adp
                .setOnItemClickListener { adapter, view, position ->
                    listener.onPopClockListener(datas[position])
                    setSele(position)
                    dismiss()
                }

    }
    public fun setSele(num:Int){
        for (i in 0..datas.size-1){
            if (i==num){
                selectHash.put(i,true)
            }else{
                selectHash.put(i,false)
            }

        }
        adp.notifyDataSetChanged()
    }

    interface OnPopClic{
        fun onPopClockListener(type:String)
    }
}